import { Controller, Get, Post, Body, Patch, Param, Delete, Query, Req, UseInterceptors, UploadedFile } from '@nestjs/common';
import { SystemService } from './system.service';
import { PublicRoute } from 'src/decorations/PublicRoute';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { wss } from 'src/config/WebSocket';
import { WebSocket } from 'ws'

@ApiTags('系统公共')
@Controller('system')
export class SystemController {
  constructor(
    private readonly systemService: SystemService,
  ) { }

  @ApiOperation({ summary: '上传文件' })
  @Post('image/upload')
  async sliceImage(@Body() body: any) {
    return this.systemService.uploadImage(body.base64)
  }

  @ApiOperation({ summary: '删除文件' })
  @Delete('image/delete/:hash')
  async deleteImage(@Param('hash') hash: string) {
    if (!hash) throw "未获取到图片 hash 标识~"
    return this.systemService.deleteImage(hash)
  }

  @PublicRoute()
  @ApiOperation({ summary: '日志调试' })
  @Post('logger')
  postLogger(@Body() body: any) {
    wss.clients.forEach((client) => {
      if (client.readyState === WebSocket.OPEN) {
        client.send(JSON.stringify(body));
      }
    });
  }
}
